package net.sf.jOpinionPoll.tag.component;

import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;

public class SimpleTitleRenderer extends Renderer {

	@Override
	public void encodeBegin(FacesContext context,
	  UIComponent component)throws IOException
	{
		ResponseWriter writer = context.getResponseWriter();
		UISimpleTitleComponent simpleTitleComponent = (UISimpleTitleComponent)component;
        
        //span
		writer.startElement("span", component);
        
        //style
        if (component.getAttributes().get("style") != null)
            writer.writeAttribute("style", simpleTitleComponent.getStyle(), null);
        else if (simpleTitleComponent.getValueExpression("style") != null)
            writer.writeAttribute("style", simpleTitleComponent.getValueExpression("style").getValue(context.getELContext()).toString(), null);
            
        //class
        if (component.getAttributes().get("styleClass") != null)
            writer.writeAttribute("class", simpleTitleComponent.getStyleClass(), null);
        else if (simpleTitleComponent.getValueExpression("styleClass") != null)
            writer.writeAttribute("class", simpleTitleComponent.getValueExpression("lineClass").getValue(context.getELContext()).toString(), null);
        
        //valor/titulo
        if (component.getAttributes().get("text") != null)
        	writer.write(simpleTitleComponent.getText());
        else if (simpleTitleComponent.getValueExpression("text") != null) 
        	writer.write(simpleTitleComponent.getValueExpression("text").getValue(context.getELContext()).toString());
        
		writer.endElement("span");
        
        //div
        writer.startElement("div", component);
        
        //line style
        if (component.getAttributes().get("lineStyle") != null)
            writer.writeAttribute("style", simpleTitleComponent.getLineStyle(), null);
        else if (simpleTitleComponent.getValueExpression("lineStyle") != null)
            writer.writeAttribute("style", simpleTitleComponent.getValueExpression("lineStyle").getValue(context.getELContext()).toString(), null);
            
        //line class
        if (component.getAttributes().get("lineStyleClass") != null)
            writer.writeAttribute("class", simpleTitleComponent.getLineStyleClass(), null);
        else if (simpleTitleComponent.getValueExpression("lineStyleClass") != null)
            writer.writeAttribute("class", simpleTitleComponent.getValueExpression("lineStyleClass").getValue(context.getELContext()).toString(), null);
            
        writer.endElement("div");
        
        //Paragrafo
        writer.startElement("p", component);
        writer.endElement("p");
        
		writer.flush();
	}
}